<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<html:header title="销售单详情">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
    </style>

    <script>
        jQuery(function () {


            ajaxSelect("${baseUrl}settAccount/settAccount/listVo.action","settAccountId");

            $("#cancel").click(function () {
                closeSubIframe();
            });
        });

        layui.use(['form', 'table', 'laydate'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate,
                util = layui.util;


            get("${baseUrl}sale/refund/getVoById.action?id=${id}", function (res) {
                if (res.code == 1) {
                    $("#print").click(function () {
                        document.getElementById("printFrame").contentWindow.printPage(res.data);
                    });

                    if (res.data.close) {
                        showCloseImage();
                    }

                    if (res.data.tax == null) {
                        $("#tax").hide();
                    } else {
                        $("#tax").show();
                        $("#tax").html(getPrice(res.data.tax));
                    }

                    if (res.data.sale == null) {
                        $("#linkNo").parent().parent().hide();
                    } else {
                        $("#linkNo").parent().parent().show();
                        $("#linkNo").html(res.data.sale.no);
                        $("#linkNo").click(function () {
                            openTab("销售单详情","${baseUrl}sale/saleDetail.action?id="+res.data.sale.id);
                        });
                    }
                    res.data.realAmt = getPrice(res.data.realAmt);
                    formSetVal("form",res.data);

                    $("#no").html(res.data.no);
                    $("#totalAmount").html(res.data.totalAmt);
                    $("#house").html(res.data.houseVo.store.name+">"+res.data.houseVo.name);
                    $("#custom").html(res.data.custom.name);

                    $("#type").html(res.data.type == 1 ? "批发":"销售");
                    $("#user").html(res.data.user.name);
                    $("#date").html(GMTToDate(res.data.date));

                    $("#otherAmt").html(getPrice(res.data.otherAmt));
                    $("#remark").html(res.data.remark);

                    $("#makeUser").html(res.data.makeUser.name);
                    $("#makeTime").html(res.data.makeTime);

                    parseImageNames(res.data.image);

                    let list=[];
                    $.each(res.data.refundGoodsVoList, function (i,d) {
                        let vo = d.goodsVo;
                        vo.goodsId = d.goodsId;
                        vo.number = d.number;
                        vo.price = d.price;
                        vo.unitId = d.unitId;
                        vo.times = d.times;
                        vo.amount = d.amount;
                        vo.remark = d.remark;
                        list.push(vo);
                    });

                    $.each(res.data.refundPacketVoList, function (i,d) {
                        let vo = d;
                        vo.packetId = d.packetId;
                        vo.no = d.packetNo;
                        vo.name = d.packetName;
                        vo.number = d.number;
                        vo.price = d.price;
                        vo.amount = d.amount;
                        vo.remark = d.remark;
                        list.push(vo);
                    });
                    renderTable(list);
                }else {
                    tip("获取订单信息失败");
                }
            });
        });

        function renderTable(list) {
            layui.table.render({
                id: 'table',
                elem: '#table',
                data: list,
                totalRow:true,
                text: {
                    none: '未选择商品' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                },
                cols: [[
                    {type: 'numbers'},
                    {field: 'name', minWidth: 140, title: '名称/规格', style: "font-size:12px;", templet: '#nameBar'},
                    {field: 'no', width: 140, title: '商品编号', style: "font-size:12px;"},
                    {width: 120, title: '属性', style: "font-size:12px;", templet: function (d) {
                            return genAttr(d.attrNameVoList);
                        }},
                    {field:'number', width: 110, title: '数量',templet:'#numberBar'},
                    {width: 120, title: '单位', style: "font-size:12px;", templet: function (d) {
                            return d.goodsId==null ? "": genUnitName(d.goodsUnitVoList,d.unitId);
                        }},
                    {field: 'price', width: 120, title: '单价', templet: function (d) {
                            return getPrice(d.price);
                        }},
                    {field: 'amount', width: 120, title: '金额', style: "font-size:12px;", templet: function (d) {
                            return getPrice(d.amount);
                        }},
                    {field:'snNumber', width: 110, title: '序列号',templet: "#snNumberBar"},
                    {field: 'remark', width: 120, title: '备注', style: "font-size:12px;"},

                ]],
                limit: Number.MAX_VALUE,
                done: function (res, curr, count) {
                    processTotalRow();
                    $(".packetName").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table;
                        showPacketList(tableData[index].name, tableData[index].packetGoodsVoList);
                    });
                    $(".snNumberDiv").unbind().bind('click',function () {
                        let index = $(this).parents('tr').data('index');
                        let tableData = layui.table.cache.table;
                        showSnList(tableData[index].name, tableData[index].goodsSnList);
                    });
                }
            });
        }


        function processTotalRow() {
            let amount=0;
            let number = 0;
            $.each(layui.table.cache.table, function (i,d) {
                number += parseInt(d.number);
                amount += parseFloat(d.amount);
            });
            setTotalRow("amount",getPrice(amount));
            setTotalRow("number",number);
            setTotalRow("0","合计");
        }
    </script>

</html:header>

<iframe id="printFrame" style="width:0px;height:0px" src="${baseUrl}print/refundPrint.action" frameborder="0"></iframe>
<body>

<div class="layui-inline" style="margin-bottom: 20px;">
    <a class="layui-btn" id="print">打印</a>
    <a class="layui-btn layui-btn-primary" id="cancel">取消</a>
</div>
<div class="layuimini-container">
    <div class="layuimini-main">
        <html:closeOrderImage/>

        <h2 class="tableBeforeTitle">销售退货单</h2>
        <div style="height: 4px;background-color: #bbbbbb;width: 100%;margin: 20px 0 2px 0;"></div>
        <div style="height: 1px;width: 100%;background-color: #dddddd;margin-bottom: 10px;"></div>
        <div style="height: 10px;"></div>

        <form class="layui-form" action="" lay-filter="form">
            <input type="hidden" name="id" value="${id}">

            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">编号：</span>
                    <span id="no" name="no"></span>
                </div>
            </div>

            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">关联单号：</span>
                    <a id="linkNo" href="#"></a>
                </div>
            </div>

            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">销售类型：</span>
                    <span id="type"></span>
                </div>
            </div>

            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">客户：</span>
                    <span id="custom"></span>
                </div>
            </div>


            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">入库仓库：</span>
                    <span id="house"></span>
                </div>
            </div>

            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">经手人：</span>
                    <span id="user"></span>
                </div>
            </div>

            <div class="layui-inline" style="margin:0 20px 10px 0;">
                <div>
                    <span style="color: #aaaaaa">业务日期：</span>
                    <span id="date"></span>
                </div>
            </div>

            <div style="height: 10px;"></div>

            <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
            <div style="overflow: auto;margin-top: -10px;">
                <table class="layui-table" style="margin: 0; min-width: 600px;">
                    <colgroup>
                        <col>
                        <col width="300">
                    </colgroup>
                    <tbody>
                    <tr>
                        <td>
                            <div style="float: right;">
                                <span style="color: #aaaaaa;padding-left: 20px;">税额：</span><span id="tax"></span>
                                <span style="color: #aaaaaa;padding-left: 20px;">其他费用：</span><span id="otherAmt"></span>
                            </div>
                        </td>

                        <td style="background-color: #FFFFCC;">
                            <div style="display: inline-block;height: 38px;line-height: 38px;width: 80px;">结算账户</div>
                            <div class="layui-input-inline" style="margin: 0;width: 160px;">
                                <select name="settAccountId" lay-filter="settAccountId" disabled>
                                </select>
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <td>
                            <div style="text-align: right;font-size:16px;font-weight:bold;height: 38px;line-height: 38px;padding-right: 60px;">
                                总计金额:<span id="totalAmount" discountFlag="1" style="color: #FF5722">0</span>元
                            </div>
                        </td>

                        <td rowspan="2" style="background-color: #FFFFCC;max-width: 300px;">
                            <div style="display: inline-block;height: 38px;line-height: 38px;width: 80px;">实退金额</div>
                            <div class="layui-input-inline" style="width: 160px;">
                                <input type="number" readonly id="realAmt" name="realAmt" class="layui-input" >
                            </div>
                        </td>

                    </tr>

                    <tr>
                        <td>
                            <div style="position: relative;">
                                <span style="color: #aaaaaa;padding-left: 20px;">备注：</span><span id="remark"></span>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>

            <div class="layui-form-item" style="padding-top: 20px;">
                <label class="layui-form-label">图片附件</label>
                <html:imageAnnex type="refund" update="true"
                                 uploadUrl="${baseUrl}sale/refund/uploadImg.action"
                                 updateUrl="${baseUrl}sale/refund/updateImage.action"/>
            </div>

            <hr>

            <span style="color: #aaaaaa;">制单人：</span><span id="makeUser"></span>
            <span style="color: #aaaaaa;padding-left: 20px;">制单时间：</span><span id="makeTime"></span>
        </form>
    </div>

    <script type="text/html" id="nameBar">
        {{#  if(d.goodsId==null){ }}
        <div class="packetName">
            {{d.name}}
        </div>
        {{#  }else{ }}
        {{genGoodsName(d.name,d.norms)}}
        {{#  } }}
    </script>

    <script type="text/html" id="numberBar" >
        <span class="number">{{ getNumber(d.number) }}</span>
        {{# if(d.sn==true){ }}
        <span class="numberSn">sn</span>
        {{# } }}
    </script>

    <script type="text/html" id="snNumberBar" >
        {{# if(d.sn==true){ }}
        {{# if(d.goodsSnList!=undefined && d.goodsSnList.length > 0){ }}
        <div class="snNumberDiv">{{d.goodsSnList.length}}个</div>
        {{# } }}
        {{# } }}
    </script>
</div>

</body>

<html:packetListDialog/>
<html:snListDialog/>
<html:loading/>
</html>